<?php
namespace Backend\Controllers;

use Models\Wx;
use Models\Alipay;

class ConfigController extends BaseController
{
    public function wxAction()
    {
        $Cuser = $this->Cuser;
        if ($this->request->isPost() == true) {
            $wx = Wx::findFirstByuid($Cuser->uid);
            if ($wx) {
                if ($wx->update($this->request->getPost())) {
                    $this->flash->warning("更新成功");
                } else {
                    $this->flash->warning("更新失败");
                }
            } else {
                $wx = new Wx();
                $wx->token = $wx->get_token();
                $wx->uid = $Cuser->uid;
                if ($wx->create($this->request->getPost())) {
                    $this->flash->warning("保存成功");
                } else {
                    $this->flash->warning("保存失败");
                }
            }
        }
        $wx = Wx::findFirstByuid($Cuser->uid);
        $this->view->setVar("wx", $wx);
    }

    public function alipayAction()
    {
        $Cuser = $this->Cuser;
        if ($this->request->isPost() == true) {
            $alipay = Alipay::findFirstByuid($Cuser->uid);
            if ($alipay) {
                if ($alipay->update($this->request->getPost())) {
                    $this->flash->warning("更新成功");
                } else {
                    $this->flash->warning("更新失败");
                }
            } else {
                $alipay = new Alipay();
                $alipay->uid = $Cuser->uid;
                if ($alipay->create($this->request->getPost())) {
                    $this->flash->warning("保存成功");
                } else {
                    $this->flash->warning("保存失败");
                }
            }
        }
        $alipay = Alipay::findFirstByuid($Cuser->uid);
        $this->view->setVar("alipay", $alipay);
        $this->view->setVar("key", $this->config->alipayFuwuRsaPublicKey);
    }
}